package com.example.babymonitor;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class recieverView extends Activity {
	
	String IPadress;
	Boolean startStream = false;
	Socket socket;
	ImageView image = null;
	Boolean picStream = true;
	Handler myHandler = new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_recieverview);
		
		EditText IP = (EditText)findViewById(R.id.recieverView_EnterIP);
		IPadress = IP.toString();
		
		Button startButton = (Button) findViewById(R.id.recieverView_StartButtton);
		startButton.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				//while(startStream) {
					picStream = true;
				myHandler.postDelayed(mMyRunnable, 3000);
						updatePics();
				Log.d("pressing","start");
				
					
			}
			
		});
		
		Button stopButton = (Button) findViewById(R.id.recieverView_StopButton);
		stopButton.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				picStream = false;
				
			}
			
		});
		
		
		
		
	}
	private Runnable mMyRunnable = new Runnable()
	{
	    @Override
	    public void run()
	    {
	    	if(picStream) {
	    	myHandler.postDelayed(mMyRunnable, 4000);
			updatePics();
	    	}
	    }
	 };
	
	public void updatePics() {
		Log.d("hello","updatePics");

		try {
			//if(socket == null) {
			
			
				ThreadPolicy tp = ThreadPolicy.LAX;
				StrictMode.setThreadPolicy(tp);
			socket = new Socket("194.47.40.118",12345); 
			//}
	      // while(startStream) {
	        //outgoing stream redirect to socket
	       // OutputStream out = socket.getOutputStream();
	    	// if(picSet == false) {
	        InputStream input = socket.getInputStream();
	        Bitmap bmp=BitmapFactory.decodeStream(input);
	        image=(ImageView)findViewById(R.id.imageView1);
	        image.setImageBitmap(bmp);
	        image = null;
	        
	    	// }
	        //picSet = false;
		
	       //}
	        //Close connection
	        socket.close();
			//}
			
	       
	} catch (UnknownHostException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	} catch (IOException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	}
		
		}
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
